package com.tidu.disruptor.testDemo;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.concurrent.Executors;

public class LongEventMain {
    public static void main(String[] args) {
        // 指定事件工厂
        LongEventFactory factory = new LongEventFactory();

        // 指定 ring buffer字节大小, 必须是2的N次方
        int bufferSize = 1024;

        //单线程模式，获取额外的性能
        Disruptor<LongEvent> disruptor = new Disruptor<>(factory,
                bufferSize, Executors.defaultThreadFactory(),
                ProducerType.SINGLE,
                new YieldingWaitStrategy());

        //设置事件业务处理器---消费者
//        disruptor.handleEventsWith(new eventHandler1(), new eventHandler2())
//                        .then(new eventHandler3(), new eventHandler4());

//        disruptor.handleEventsWith(new eventHandler1(), new eventHandler2());
//        disruptor.handleEventsWith(new eventHandler3(), new eventHandler4());

//        disruptor.handleEventsWithWorkerPool(new workHandler1(), new workHandler2())
//                .thenHandleEventsWithWorkerPool(new workHandler3(), new workHandler4());

        disruptor.handleEventsWithWorkerPool(new workHandler1(), new workHandler2());
        disruptor.handleEventsWithWorkerPool(new workHandler3(), new workHandler4());

        //启动disruptor线程
        disruptor.start();
        // 获取 ring buffer环，用于接取生产者生产的事件
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();

        //为 ring buffer指定事件生产者
        LongEventProducerWithTranslator producer = new LongEventProducerWithTranslator(ringBuffer);
        //循环遍历
        for (int i = 1; i <= 4; i++) {
            //发布数据
            producer.onData((long)i);
        }
        //停止disruptor线程
        disruptor.shutdown();
    }
}